'use strict'
const fs = require('fs');
const { clearScreenDown } = require('readline');

class sticker {

    constructor() {
        this.param = '/*'
    }
    //创建资源
    async get(c) {
        var url = c.param.starPath;
        ////www.h5fd.xyz:2021/editor/template/user_id
        if (url !== "" && url.indexOf("&") == -1) {
            var user_id = url;
            var select = [];
            try {
                
                let sqltext1 = "SELECT user_pay_template FROM users WHERE user_id = " + `'${user_id}'`;
                var r1 = await c.service.psql.query(sqltext1);
                var r2 = r1.rows;//r1.rows = [ { user_pay_template: 'user_1_pay_tem,'user_2_pay_tem' } ]
                var str = r2[0].user_pay_template;
                var arr = str.split(",");
                for (var i = 0; i < arr.length; i++) {
                    var patTem = arr[i];
                    let sqltext2 = "SELECT template_id FROM " + `${patTem}`;
                    var sql2 = await c.service.psql.query(sqltext2);//sql2.rows=[{template_id:7}][{template_id:4}]
                    var temId = sql2.rows;
                    var dyx = temId[0].template_id;
                    let sqltext3 = "SELECT * FROM template WHERE template_id = " + `${dyx}`;
                    var r3 = await c.service.psql.query(sqltext3);
                    console.log(r3.rows);
                    select.push(r3.rows);
                }
                let sqltext4 = "SELECT * FROM template";
                var r4 = await c.service.psql.query(sqltext4);
                select.push(r4.rows);
                c.send(select);

            } catch (err) {
                console.log(err);
                c.status(400).send('error')
            }
        }

        //www.h5fd.xyz:2021/editor/template/user_id&template_id
        if (url.indexOf("&") !== -1) {
            try {
                var url1 = url.split('&');
                var user_id = url1[0];
                var template_id = url1[1];
                let sqltext1 = "SELECT template_sticker FROM template WHERE template_id = " + `${template_id}`;

                var r1 = await c.service.psql.query(sqltext1)
                if (r1.rowCount === 0) {
                    return c.send('this template have no sticker')
                }
                var r2 = JSON.stringify(r1.rows);
                var r3 = r2.split(":");
                var r4 = r3[1].split("}");
                var r5 = r4[0].split('"');
                var template_sticker_id = r5[1];//template_sticker_id为template_1_sticker表名


                let sqltext2 = `SELECT * FROM ` + `${template_sticker_id}`;
                //console.log("1");
                var r6 = await c.service.psql.query(sqltext2);
                //console.log("2");
                if (r6.rowCount === 0) {
                    return c.status(404).send('user not found')
                }
                var a = [];
                a.push(r6.rows);

                for (var i = 0; i < r6.rows.length; i++) {
                    var j = r6.rows[i];
                    //console.log(j);
                    var k = j.sticker_id;
                    //j.stikcer_id为模板所用贴纸的id
                    //console.log(k);
                    //console.log("3");
                    let sqltext3 = `SELECT * FROM sticker WHERE sticker_id = ` + `'${k}'`;
                    //console.log("4");
                    var s = await c.service.psql.query(sqltext3);
                    //console.log("5");
                    if (s.rowCount === 0) {
                        return c.status(404).send('sticker not found')
                    }
                    a.push(s.rows);
                }
                //console.log("6");
                c.send(a);

            }
            catch (err) {
                console.log(err);
                c.status(400).send('error')
            }
            return
        }
    }

}


module.exports = sticker